package com.drore.logapp.http.interceptor;

import com.drore.app.utils.LogUtils;
import com.drore.logapp.base.BaseApplication;
import com.drore.logapp.utils.SpKeyUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * Created by drore on 17/3/16.
 */

public class SaveCookiesInterceptor implements Interceptor {
    private String TAG = "SaveCookiesInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());

        if (!originalResponse.headers("request-token").isEmpty()) {
            LogUtils.e(TAG, "Save  Cookies   success");
            String cookies = "";
            for (String header : originalResponse.headers("request-token")) {
                cookies = header;
            }
            //更新token值
            BaseApplication.getSp().putString(SpKeyUtils.request_token, cookies);
        } else {
            LogUtils.e(TAG, "Save   Cookies  error");
        }

        return originalResponse;
    }
}
